/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.spring.factory;

import org.hibernate.HibernateException;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.SaveOrUpdateEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.persistence.bean.Persistable;
import com.genia.toolbox.persistence.spring.manager.DependencyManager;

/**
 * implementation of {@link org.hibernate.SaveOrUpdateEventListener} to allow
 * the {@link DependencyManager} to do its job.
 */
@SuppressWarnings("serial")
public class HibernateSaveOrUpdateEventListener
    implements SaveOrUpdateEventListener

{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(HibernateSaveOrUpdateEventListener.class);

  /**
   * the {@link DependencyManager} to use.
   */
  private DependencyManager dependencyManager;

  /**
   * the real {@link SaveOrUpdateEventListener}.
   */
  private SaveOrUpdateEventListener saveOrUpdateEventListener;



  /**
   * getter for the dependencyManager property.
   * 
   * @return the dependencyManager
   */
  public DependencyManager getDependencyManager()
  {
    return dependencyManager;
  }



  /**
   * setter for the dependencyManager property.
   * 
   * @param dependencyManager
   *          the dependencyManager to set
   */
  public void setDependencyManager(DependencyManager dependencyManager)
  {
    this.dependencyManager = dependencyManager;
  }



  /**
   * handle the creation dependency of an object.
   * 
   * @param object
   *          the object being created
   * @return wether this object has been created or updated
   */
  private boolean onModify(Object object)
  {
    if (object instanceof Persistable<?>) {
      Persistable<?> persistable = (Persistable<?>) object;
      try {
        if (persistable.getIdentifier() == null) {
          getDependencyManager().onCreate(persistable);
          return true;
        }
        else {
          getDependencyManager().onUpdate(persistable);
        }
      }
      catch (BundledException e) {
        LOGGER.error(e.getMessage(), e);
        throw new HibernateException(e);
      }
    }
    return false;
  }



  /**
   * handle the post creation dependency of an object.
   * 
   * @param object
   *          the object being created
   */
  private void onAfterCreation(Object object)
  {
    if (object instanceof Persistable<?>) {
      Persistable<?> persistable = (Persistable<?>) object;
      try {
        getDependencyManager().onPostCreate(persistable);
      }
      catch (BundledException e) {
        LOGGER.error(e.getMessage(), e);
        throw new HibernateException(e);
      }
    }
  }



  /**
   * handle the post creation dependency of an object.
   * 
   * @param object
   *          the object being created
   */
  private void onAfterUpdate(Object object)
  {
    if (object instanceof Persistable<?>) {
      Persistable<?> persistable = (Persistable<?>) object;
      try {
        getDependencyManager().onPostUpdate(persistable);
      }
      catch (BundledException e) {
        LOGGER.error(e.getMessage(), e);
        throw new HibernateException(e);
      }
    }
  }



  /**
   * handle the given update event.
   * 
   * @param event
   *          the update event to be handled.
   * @see org.hibernate.event.SaveOrUpdateEventListener#onSaveOrUpdate(org.hibernate.event.SaveOrUpdateEvent)
   */
  public void onSaveOrUpdate(SaveOrUpdateEvent event)
  {
    boolean create = onModify(event.getObject());
    saveOrUpdateEventListener.onSaveOrUpdate(event);
    if (create) {
      onAfterCreation(event.getObject());
    }
    else {
      onAfterUpdate(event.getObject());
    }
  }



  /**
   * getter for the saveOrUpdateEventListener property.
   * 
   * @return the saveOrUpdateEventListener
   */
  public SaveOrUpdateEventListener getSaveOrUpdateEventListener()
  {
    return saveOrUpdateEventListener;
  }



  /**
   * setter for the saveOrUpdateEventListener property.
   * 
   * @param saveOrUpdateEventListener
   *          the saveOrUpdateEventListener to set
   */
  public void setSaveOrUpdateEventListener(SaveOrUpdateEventListener saveOrUpdateEventListener)
  {
    this.saveOrUpdateEventListener = saveOrUpdateEventListener;
  }

}
